#!/usr/bin/python
import sys
import os
import getopt

def usage():
    print ""
    print ""
    print "reverse v0.2.1 By Bewiwi"
    print "OPTION"
    print "-o --output"
    print "    fichier de sortie"
    print ""
    print "-h --help"
    print "    affiche ce menu"
    print ""
    print "UTILISATION"
    print "    reverse /home/user/text.txt -o /home/user/fichierinverser.txt"
    print ""
    
def createfile(nom):
    return file(nom,'w')



#definition des variables
count = 0
li = []
j = 0
out = 0

try:
    opts, args = getopt.getopt(sys.argv[1:], "oh",["output=","help"])
except getopt.GetoptError:          
    sys.exit()

for opt, arg in opts:
    if opt in ("-h", "--help"):
            usage()
            sys.exit()
    elif opt in ("-o", "--output"):
            f_output = createfile(arg)
            out = 1

try:            
    finput = sys.argv[1]
except:
    usage()
    sys.exit()
        
        
#gestion des fichiers
f_input = open(finput,'r')
for line in f_input:
    li.append(line)
    count = count+1

count = -1*count

j=-1

while j != count-1:
    if out == 1:
        f_output.write(li[j])
    elif out == 0:
        sys.stdout.write(li[j])
    j=j-1
f_input.close()
if out == 1:
    f_output.close()
sys.exit()